﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using P3SS.Engine;
using P3SS.Settings;
using P3SS.Networking;
using P3SS.FailedMail;
using P3SS.Service;

namespace P3SS.Testbed
{
	class Program
	{
		static void Main(string[] args)
		{
			DoEngineWork();
		}

		private static void DoEngineWork()
		{
			ServiceEngine engine = new ServiceEngine(null);
			engine.LogEntryAdded += new EventHandler<LogEntryEventArgs>(engine_LogEntryAdded);
			engine.Start();
			Console.WriteLine("Engine started");
			Console.ReadLine();
		}

		static void engine_LogEntryAdded(object sender, LogEntryEventArgs e)
		{
			Console.WriteLine(string.Format(">>{0}: {1}", e.Type, e.Message));
		}

		private static void DoOwnWork()
		{
			ServiceSettings mySettings = ServiceSettings.LoadSettings();
			FailedMailEngine failedMail = FailedMailEngine.LoadFailedMail();
			Dictionary<string, SmtpEngine> smtpNames = new Dictionary<string, SmtpEngine>();
			mySettings.LoadUIDLs();

			foreach (Account thisAccount in mySettings.Accounts)
			{
				thisAccount.Pop3Account.MessageOccurred += new EventHandler<EngineMessageEventArgs>(Pop3Account_MessageOccurred);
				thisAccount.SmtpAccount.MessageOccurred += new EventHandler<EngineMessageEventArgs>(SmtpAccount_MessageOccurred);

				//List<string> mailMessages = thisAccount.Pop3Account.GetAllMail();
				//failedMail.AddItems(thisAccount.SmtpAccount.SubmitAllMail(mailMessages));

				string mySmtpName = thisAccount.SmtpAccount.ToShortString();
				if (!smtpNames.ContainsKey(mySmtpName))
				{
					smtpNames.Add(mySmtpName, thisAccount.SmtpAccount);
				}
			}

			List<FailedMailItem> failedToRetry = failedMail.PopAllItems();
			failedMail.AddItems(RetryFailedMessages(failedToRetry, smtpNames));

			//ServiceSettings.SaveSettings(mySettings);
			mySettings.SaveUIDLs();
			FailedMailEngine.SaveFailedMail(failedMail);

			Console.WriteLine("All Done");
			Console.ReadLine();
		}

		static void SmtpAccount_MessageOccurred(object sender, EngineMessageEventArgs e)
		{
			Console.WriteLine("SMTP " + e.TypeOfMessage.ToString() + " " + e.Message);
		}

		static void Pop3Account_MessageOccurred(object sender, EngineMessageEventArgs e)
		{
			Console.WriteLine("POP3 " + e.TypeOfMessage.ToString() + " " + e.Message);
		}

		private static List<FailedMailItem> RetryFailedMessages(List<FailedMailItem> mailToRetry, Dictionary<string, SmtpEngine> smtpNames)
		{
			List<FailedMailItem> mailStillFailing = new List<FailedMailItem>();

			while (mailToRetry.Count > 0)
			{
				if (smtpNames.ContainsKey(mailToRetry[0].SMTPServer))
				{
					try
					{
						smtpNames[mailToRetry[0].SMTPServer].ResendFailedMail(mailToRetry[0]);
					}
					catch (Exception ex)
					{
						mailToRetry[0].FailureReason = ex.ToString();
						mailStillFailing.Add(mailToRetry[0]);
					}
				}
				else
				{
					mailToRetry[0].FailureReason = "SMTP server for message is missing";
					mailStillFailing.Add(mailToRetry[0]);
				}

				mailToRetry.RemoveAt(0);
			}

			return mailStillFailing;
		}
	}
}
